package com.iussoft.receiveData.interceptor;

import com.iussoft.tools.PkgScanner;
import com.iussoft.tools.SpringContextLoader;
import com.iussoft.tools.SpringHandlerInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description: 拦截器配置实现类
 *
 * @author dyh 创建时间：2018年07月02日
 */
@Configuration
public class WebInterceptorConfig implements  WebMvcConfigurer {

    /**
     * 自己定义的拦截器类
     * @return
     */
    private List<String> customInterceptor() {
        try {
            PkgScanner scanner = new PkgScanner("com.iussoft.receiveData.interceptor", SpringHandlerInterceptor.class);
            return scanner.scan();
        }catch (IOException e){
            return null;
        }
    }

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> interceptors = customInterceptor();
        if(interceptors==null){
            interceptors = new ArrayList();
            interceptors.add("com.iussoft.receiveData.interceptor.SearchInterceptor");
        }
        if(null != interceptors){
            for(int i=0;i<interceptors.size();i++){
                String name = interceptors.get(i);
                try {
                    Class clazz = Class.forName(name);
                    HandlerInterceptor handlerInterceptor = (HandlerInterceptor)clazz.newInstance();
                    SpringHandlerInterceptor annotation = AnnotationUtils
                            .findAnnotation(handlerInterceptor.getClass(), SpringHandlerInterceptor.class);
                    Field[] fields = clazz.getSuperclass().getDeclaredFields();
                    for (Field field : fields) {
                        Qualifier qualifier = field.getAnnotation(Qualifier.class);
                        if(qualifier != null){
                            field.set(handlerInterceptor, SpringContextLoader.getBean(qualifier.value()));
                        }
                    }
                    fields = clazz.getFields();
                    for (Field field : fields) {
                        Qualifier qualifier = field.getAnnotation(Qualifier.class);
                        if(qualifier != null){
                            field.set(handlerInterceptor, SpringContextLoader.getBean(qualifier.value()));
                        }
                    }
                    InterceptorRegistration registration = registry.addInterceptor(handlerInterceptor);

                    String[] includePatterns = getIncludePatterns(annotation);
                    if (includePatterns.length != 0) {
                        registration.addPathPatterns(includePatterns);
                    }
                    String[] excludePatterns = getExcludePatterns(annotation);
                    if (excludePatterns.length != 0) {
                        registration.excludePathPatterns(excludePatterns);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private String[] getIncludePatterns(SpringHandlerInterceptor annotation) {
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(annotation.includePatterns()));
        return list.toArray(new String[] {});
    }

    private String[] getExcludePatterns(SpringHandlerInterceptor annotation) {
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(annotation.excludePatterns()));
        return list.toArray(new String[] {});
    }
}
